<template>
  <div>
    <div style="display: flex;justify-content: center">
      <h1>供应商管理</h1>
    </div>
    <!--    表格-->
    <div >
      <el-table :data=cutBean.records  style="width: 100%" >
        <el-table-column prop="name" label="供应商名称" />
        <el-table-column prop="leadingCadre" label="负责人" />
        <el-table-column prop="supRange" label="经营范围"/>
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button type="danger" @click="findById(scope.row.id)">查看</el-button>
            <el-button type="success" @click="changeVisible=true,show(scope.row.id)">修改信息</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:currentPage="cutBean.current"
          v-model:page-size="cutBean.size"
          background
          layout="prev, pager, next, jumper"
          :total="cutBean.total"
          @current-change="findByItem"
      />
    </div>

    <!--    条件查询-->
    <div>
      <div style="display: flex;justify-content: space-around;margin-top: 20px">
        <div>供应商名称<el-input v-model="findObj.name"/></div>
        <div>经营范围<el-input v-model="findObj.supRange"/></div>
        <div><el-button type="primary" @click="findByItem(1)" style="margin-top: 16px">查询</el-button></div>
        <div style="margin-top: 16px">
          <el-button type="success" @click="dialogVisible=true">添加供应商</el-button>
          <el-button type="success" @click="addAllVisible=true">excel上传</el-button>
        </div>
      </div>

      <!--      添加信息弹出框-->
      <el-dialog
          v-model="dialogVisible"
          title="添加供应商信息"
          width="30%"
      >
        <el-form
            ref="userForm"
            :model="addObj"
            :rules="rules"
            label-width="120px"
            class="demo-ruleForm"
            status-icon
            style="width: 400px;"
        >
          <el-form-item label="供应商名称：" prop="">
         <el-input v-model="addObj.name"/>
          </el-form-item>
          <el-form-item label="负责人：" prop="">
            <el-input  v-model="addObj.leadingCadre"/>
          </el-form-item>
          <el-form-item label="负责人电话：" prop="">
            <el-input  v-model="addObj.phone"/>
          </el-form-item>
          <el-form-item label="供应商地址：" prop="">
            <el-input  v-model="addObj.address"/>
          </el-form-item>
          <el-form-item label="经营范围：" prop="">
            <el-input  v-model="addObj.supRange"/>
          </el-form-item>

        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false,addClient()">添加</el-button>
      </span>
        </template>
      </el-dialog>
      <!--addAll-->

      <el-dialog
          v-model="addAllVisible"
          title="上传文件"
          width="40%"
      >
        <el-row style="width: 100%;line-height: 40px">

          <input type="file" ref="textFile">
          <br>

          <el-button @click="addAllVisible=false,addAll()">导入</el-button>

        </el-row>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="addAllVisible = false">确定</el-button>
      </span>
        </template>
      </el-dialog>

      <!--      查看弹出框-->
      <el-dialog
          v-model="infoVisible"
          title="添加供应商信息"
          width="30%"
          style="line-height: 30px;position: relative"

      >
        <p >供应商名称:<span style="margin-left: 50px">{{supplierObj.name}}</span></p>
        <p>负责人:<span style="margin-left: 50px">{{supplierObj.leadingCadre}}</span></p>
        <p>负责人电话:<span style="margin-left: 50px">{{supplierObj.phone}}</span></p>
        <p>供应商地址:<span style="margin-left: 50px">{{supplierObj.address}}</span></p>
        <p>经营范围:<span style="margin-left: 50px">{{supplierObj.supRange}}</span></p>

        <template #footer>
      <span class="dialog-footer">
        <el-button @click="infoVisible = false">确定</el-button>
      </span>
        </template>
      </el-dialog>
      <!--      修改弹出框-->
      <el-dialog
          v-model="changeVisible"
          title="修改供应商信息"
          width="30%"
      >
        <el-form
            ref="supplierForm"
            :model="updateObj"
            :rules="rules"
            label-width="120px"
            class="demo-ruleForm"
            status-icon
            style="width: 400px;"
        >
          <el-form-item label="供应商名称：" prop="">
            {{updateObj.name}}
          </el-form-item>
          <el-form-item label="负责人：" prop="">
            <el-input style="width: 200px" v-model="updateObj.leadingCadre"/>
          </el-form-item>
          <el-form-item label="负责人电话：" prop="">
            <el-input style="width: 200px" v-model="updateObj.phone"/>
          </el-form-item>
          <el-form-item label="供应商地址：" prop="">
            {{updateObj.address}}
          </el-form-item>
          <el-form-item label="经营范围：" prop="">
            {{updateObj.supRange}}
          </el-form-item>

        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="update()">修改</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      supplierObj: {},
      cutBean: [],
      findObj:{},
      dialogVisible: false,
      infoVisible:false,
      changeVisible:false,
      addAllVisible:false,
      fromObj:{},
      addObj:{},
      updateObj:{},
      }
    },
  methods:{
    findByItem(pageNO){
      this.findObj.pageNO=pageNO;
      axios.get("/project/supplier/findByItem",{params:this.findObj}).then(resp=>{
        this.cutBean=resp.data;
      })
    },
    findById(id){
      this.infoVisible=true
      axios.get("/project/supplier/findById",{params:{id}}).then(resp=>{
        this.supplierObj=resp.data;
      })
    },
    show(id){
      this.changeVisible=true
      axios.get("/project/supplier/findById",{params:{id}}).then(resp=>{
        this.updateObj=resp.data;
      })

    },
    update(){
      axios.get("/project/supplier/update",{
        params:{
          id:this.updateObj.id,
          name:this.updateObj.leadingCadre,
          phone:this.updateObj.phone
        }
      }).then(resp=>{
        if (resp.data == "ok"){
          this.changeVisible=false
          this.findByItem(1);
        }
      })
    },
    addClient() {
      this.dialogVisible=true;
      axios.get("/project/supplier/add", {params: this.addObj}).then(resp => {
        if (resp.data == "ok") {
          this.dialogVisible=false;
          this.findByItem(1);
        }
      });
    },
    async addAll() {
      var fromObj = new FormData();
      fromObj.append("excelFile", this.$refs.textFile.files[0]);
      let config = {
        headers: {'Content-Type': 'multipart/form-data'}
      }
      await axios.post("/project/supplier/addList", fromObj, config).then(resp => {
        if (resp.data == "ok") {
          this.findByItem(1);
        }
      });
    }

  },
  created() {
    this.findByItem(1);
  }
}
</script>

<style scoped>

</style>